package com.mo.star.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mo.star.pojo.Message;
import com.mo.star.vo.ChatCard;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author 云赋
* @description 针对表【message(消息实体表)】的数据库操作Mapper
* @createDate 2024-09-06 11:38:13
* @Entity generator.domain.Message
*/
@Mapper
public interface MessageMapper extends BaseMapper<Message> {
    @Select("select \n" +
            "u.avatarUrl,m.content,max(m.sendTime) currentTime ,u.username,u.id userid\n" +
            "from message m join usertable u on m.receiveUserId = u.id \n" +
            " where m.sendUserId = #{loginUserId} or m.receiveUserId = #{loginUserId} " )
    List<ChatCard> seChatCart2(String loginUserId);

    @Select("SELECT distinct us.avatarUrl, us.id userid,us.username  from contactpeople con join usertable us  on us.id = con.contactid where con.userid =#{loginUserId}" )
    List<ChatCard> seChatCart(String loginUserId);
    @Select("SELECT m.receiveUserId ,m.sendUserId ,m.content,m.sendTime,u.avatarUrl\n" +
            "FROM message m  join usertable u on u.id = m.sendUserId\n" +
            "WHERE (m.sendUserId =#{fromUserId}  AND m.receiveUserId =#{toUserId} ) \n" +
            "   OR (m.sendUserId=#{toUserId} AND m.receiveUserId=#{fromUserId} )\n" +
            "ORDER BY m.sendTime ;")
    List<Message> messageList(String fromUserId,String toUserId);
@Insert("insert into message(receiveUserId,sendUserId,content,sendTime) values (#{receiveUserId},#{sendUserId},#{content},now())")
    int doMessage(Message message);
@Insert("insert into contactpeople (userid,contactid) values (#{fromUserId},#{toUserId})")
int dochat(String fromUserId,String toUserId);
}




